package com.tengju.user.infrastructure.repository;

import com.tengju.user.domain.model.sign.StarLimitInfo;
import com.tengju.user.domain.model.sign.StarLimitRepository;
import com.tengju.user.domain.model.user.ManageUser;
import com.tengju.user.domain.model.user.Staff;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.StarBlacklistDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.custom.StarBlacklistDOCustomMapper;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.custom.StarWhiteUserDOCustomMapper;
import com.tengju.user.infrastructure.repository.convert.StarSignConvert;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Repository
public class StarLimitRepositoryImpl implements StarLimitRepository {

    @Resource
    StarBlacklistDOCustomMapper starBlacklistDOCustomMapper;

    @Resource
    StarWhiteUserDOCustomMapper starWhiteUserDOCustomMapper;

    @Override
    public List<StarLimitInfo> listStarLimit(UserIdCode idCode) {
        List<StarBlacklistDO> starBlacklistDOList = starBlacklistDOCustomMapper.listStarBlackByIdCode(idCode.getValue());
        return starBlacklistDOList.stream()
                .map(StarSignConvert::blackLimitToStarLimit)
                .collect(Collectors.toList());
    }

    @Override
    public List<StarLimitInfo> listStarLimitByIdCodes(List<UserIdCode> idCodes) {
        List<StarBlacklistDO> starBlacklistDOList = starBlacklistDOCustomMapper.listStarBlackByIdCodeList(
                idCodes.stream().filter(Objects::nonNull).map(UserIdCode::getValue).collect(Collectors.toList())
        );
        return starBlacklistDOList.stream()
                .map(StarSignConvert::blackLimitToStarLimit)
                .collect(Collectors.toList());
    }

    @Override
    public void batchUpdateStarLimit(List<StarLimitInfo> starLimitInfoList, Staff staff) {
        if (CollectionUtils.isEmpty(starLimitInfoList)) {
            return;
        }
        List<StarBlacklistDO> starBlacklistDOList = starLimitInfoList.stream()
                .map(v -> StarSignConvert.starLimitToDO(v, staff))
                .collect(Collectors.toList());
        starBlacklistDOCustomMapper.batchUpdateOrInsertStarLimit(starBlacklistDOList);
    }

    @Override
    public void addWhiteUser(UserIdCode idCode, String username) {
        starWhiteUserDOCustomMapper.addWhiteUser(idCode.getValue(), username);
    }

    @Override
    public void batchAddWhiteUser(List<UserIdCode> idCodes, String username) {
        if (CollectionUtils.isEmpty(idCodes)) {
            return;
        }
        starWhiteUserDOCustomMapper.batchAddWhiteUser(
                idCodes.stream().filter(Objects::nonNull).map(UserIdCode::getValue).collect(Collectors.toList()),
                username);
    }

    @Override
    public void batchDeleteWhiteUser(List<UserIdCode> idCodes, String username) {
        if (CollectionUtils.isEmpty(idCodes)) {
            return;
        }
        starWhiteUserDOCustomMapper.batchDeleteWhiteUser(
                idCodes.stream().filter(Objects::nonNull).map(UserIdCode::getValue).collect(Collectors.toList()),
                username);
    }

    @Override
    public void batchDeleteBlackUser(List<UserIdCode> idCodes, String username) {
        if (CollectionUtils.isEmpty(idCodes)) {
            return;
        }
        List<Long> idCodeValues = idCodes.stream()
                .filter(Objects::nonNull)
                .map(UserIdCode::getValue)
                .collect(Collectors.toList());
        starBlacklistDOCustomMapper.batchDeleteUser(idCodeValues,username);
    }


}
